package com.yndsoft.jc.ui.categorypage;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.MainActivity;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;
import com.yndsoft.jc.core.ActivitiesManager;
import com.yndsoft.jc.model.Member;
import com.yndsoft.jc.model.Order;
import com.yndsoft.jc.model.Product;
import com.yndsoft.jc.model.ProductAttribute;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;
import com.yndsoft.jc.utils.ConstantValues;
import com.yndsoft.jc.widget.CustomDialog;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by hasee on 2017/12/15.
 */

public class ProductDetailActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.product_det_pic)
    ImageView product_det_pic;
    @Bind(R.id.product_det_backBtn)
    ImageView product_det_backBtn;
    @Bind(R.id.product_main_title)
    TextView product_main_title;
    @Bind(R.id.product_subname_title)
    TextView product_subname_title;
    @Bind(R.id.product_tip)
    TextView product_tip;
    @Bind(R.id.choose_project)
    RelativeLayout choose_project;
    @Bind(R.id.entry_cart)
    LinearLayout entry_cart;
    @Bind(R.id.ll_add_to_cart)
    TextView ll_add_to_cart;

    private int id;
    CustomDialog dialog;

    @Bind(R.id.place_an_order)
    TextView place_an_order;

    @Bind(R.id.det_pic)
    WebView det_pic;

    List<ProductAttribute> list = new ArrayList<>();
    Order resultData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);

        setViews();
        setListener();
    }

    private Product product;//当前的产品

    private void setViews() {
        resultData = new Order();
        id = getIntent().getIntExtra(ConstantValues.ID_AD, 100001);
        mHttpUtil.getProductDetail(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:

                        break;
                    case 1:
                        Type type = new TypeToken<Product>() {
                        }.getType();
                        Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                        product = (Product) fromJson;
                        Glide.with(ProductDetailActivity.this).load(product.getImg()).into(product_det_pic);
                        product_main_title.setText(product.getName());
                        product_subname_title.setText(product.getSubname());
                        product_tip.setText(product.getTip());
                        if (null != product.getNote()) {
                            det_pic.loadData(getNewContent(product.getNote()), "text/html; charset=UTF-8", null);
                        }
                        break;
                }
            }
        }, "product", id);
    }

    /**
     * 将html文本内容中包含img标签的图片，宽度变为屏幕宽度，高度根据宽度比例自适应
     **/
    public static String getNewContent(String htmltext) {
        try {
            Document doc = Jsoup.parse(htmltext);
            Elements elements = doc.getElementsByTag("img");
            for (Element element : elements) {
                element.attr("width", "100%").attr("height", "auto");
            }

            return doc.toString();
        } catch (Exception e) {
            return htmltext;
        }
    }

    private void setListener() {
        product_det_backBtn.setOnClickListener(this);
        choose_project.setOnClickListener(this);
        place_an_order.setOnClickListener(this);
        entry_cart.setOnClickListener(this);
        ll_add_to_cart.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.product_det_backBtn:
                finish();
                break;
            case R.id.choose_project://获取产品属性
                openAttributeDialog(true);
                break;
            case R.id.place_an_order:
                openAttributeDialog(false);
                break;
            case R.id.entry_cart:
                MainActivity mainActivity = (MainActivity) ActivitiesManager.getInstance().getSpecialActivity(MainActivity.class);
//                ProductListActivity listActivity = (ProductListActivity) ActivitiesManager.getInstance().getSpecialActivity(ProductListActivity.class);
//                listActivity.finish();
                mainActivity.clickCart();
                finish();
                break;
            case R.id.ll_add_to_cart:
                openAttributeDialog(true);
                break;
        }
    }

    private void openAttributeDialog(final boolean toCart) {
        mHttpUtil.getProductAttribute(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        ToastUtil.show(mContext, "暂无检测项");
                        break;
                    case 1:
                        Type type1 = new TypeToken<List<ProductAttribute>>() {
                        }.getType();
                        Object fromJson1 = new Gson().fromJson(returnMsg.getInfo(), type1);
                        list = (ArrayList<ProductAttribute>) fromJson1;
//                                dialog.setList(list);
                        if (dialog != null) {
                            dialog.setToCart(toCart);
                            dialog.show();
                        } else {
                            dialog = new CustomDialog(mContext, R.style.Custom_Progress, product, list, mHttpUtil, id, mSession);
                            dialog.setToCart(toCart);
                            dialog.show();
                        }

                        break;
                }
            }
        }, "productattribute", id);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }
}
